package sf.database.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;

public class RefType implements TypeHandler<Ref> {
    public Ref get(ResultSet rs, String colName) throws SQLException {
        return rs.getRef(colName);
    }

    public Ref get(ResultSet rs, int index) throws SQLException {
        return rs.getRef(index);
    }

    @Override
    public Ref get(CallableStatement cs, int index) throws SQLException {
        return cs.getRef(index);
    }

    @Override
    public Ref get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getRef(parameterName);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        rs.updateRef(columnLabel, (Ref) value);
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        rs.updateRef(columnIndex, (Ref) value);
    }

    public Object set(PreparedStatement stat, Object obj, int i) throws SQLException {
        if (null == obj) {
            stat.setNull(i, getSqlType());
        } else {
            stat.setRef(i, (Ref) obj);
        }
        return null;
    }

    @Override
    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        throw new SQLFeatureNotSupportedException("not support");
    }

    @Override
    public int getSqlType() {
        return Types.REF;
    }

    @Override
    public Class<Ref> getDefaultJavaType() {
        return Ref.class;
    }
}
